package com.ibm.cep.test.adaptor.vehicledata

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.sql.Timestamp
import java.util.zip.ZipOutputStream;

import com.ibm.cep.util.ZipUtil;

class VehicleDataZipGeneration {
	
	private static final String MONTH = "JUN";
	
	private static final def CEP_VEHICLE_DATA_DIR = "C:\\temp\\CEP\\vehicle_data\\CI_RWS_EACH_VEHICLE_$MONTH\\";
	
	private static final def CEP_VEHICLE_DATA_ZIP_DIR = "D:\\temp\\CEP\\vehicle_data\\CI_RWS_EACH_VEHICLE_${MONTH}_archive\\";
	
	static void main(args){
		def srcFolder = new File(CEP_VEHICLE_DATA_DIR)
		def zipFolder = new File(CEP_VEHICLE_DATA_ZIP_DIR)
		if(!zipFolder.exists()){
			zipFolder.mkdirs()
		}
		def partName = "part1";
		//part 1
		def startFetchFileTime = Timestamp.valueOf('2013-02-01 00:00:00.000');
		def endFetchFileTime = Timestamp.valueOf('2013-03-06 00:00:00.000');
		
		def fileIndex = 0;
		def lineIndex = 0;
		srcFolder.eachDir {
			println it.name
			def result  = new File(zipFolder, it.name + "-" + partName + ".zip")
			ZipOutputStream zos = new ZipOutputStream(
					new BufferedOutputStream(new FileOutputStream(result, true),
							1024 * 100));
			def map = new HashMap();
			def lastName = null;
			def files = it.listFiles();
			Arrays.sort(files, new Comparator<File>(){
				int compare( arg0,  arg1) {
					return Integer.valueOf(arg0.name.replace("data", "").replace(".txt", "")).compareTo(Integer.valueOf(arg1.name.replace("data", "").replace(".txt", "")));
				};
			})
			def needContinue = true;
			files.each {
				
				def lastModifiedTime = new Timestamp(it.lastModified());
				if(endFetchFileTime.before(lastModifiedTime)){
					needContinue = false;
				}
				if(startFetchFileTime.before(lastModifiedTime)){
					//def lastFile = new File(it.parentFile, "data" + (Integer.valueOf(it.name.replace("data", "").replace(".txt", "")) - 1) + ".txt")
					if(needContinue){//&& (it.name == "data1.txt" || lastFile.exists())
//						println it.name
						map.put(it.name, it.readBytes())
						lastName = it.name;
						fileIndex ++;
						// sum total line count
						it.eachLine{
							lineIndex++;
						}
//						lineIndex --;
						
						if(map.size() >= 5){
							ZipUtil.addFileToZip(zos, map)
							map.clear();
						}
					}else{
						needContinue = false;
					}
				}
			}
			if(map.size() > 0){
				ZipUtil.addFileToZip(zos, map)
			}
			try{
				zos.close()
			}catch(Exception e){
				e.printStackTrace()
			}
			println "file count: " + fileIndex;
		}
		println "total file count: " + fileIndex;
		println "total line count: " + lineIndex;
	}

}
